package com.blog.service.Impl;

import com.blog.context.BaseContext;
import com.blog.dto.AddCategoryByWorksDTO;
import com.blog.dto.WorksDTO;
import com.blog.dto.WorksShowDTO;
import com.blog.entity.Category;
import com.blog.entity.Works;
import com.blog.mapper.WorksCategoryMapper;
import com.blog.mapper.WorksMapper;
import com.blog.service.WorksService;
import com.blog.utils.StrUtil;
import com.blog.vo.CategoryVO;
import com.blog.vo.WorksVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class WorksServiceImpl implements WorksService {

    @Autowired
    private WorksMapper worksMapper;
    @Autowired
    private WorksCategoryMapper worksCategoryMapper;

    @Override
    public List<WorksVO> getWorksList() {
        Long currentId = BaseContext.getCurrentId();
        List<Works> worksList = worksMapper.selectById(currentId);
        List<WorksVO> worksVOList = new ArrayList<>();
        worksList.forEach(works -> {
            WorksVO worksVO = new WorksVO();
            BeanUtils.copyProperties(works, worksVO);
            Timestamp createTime = works.getCreateTime();
            //格式化时间：封装好的工具
            String timeStr = StrUtil.formattedTime(createTime);
            worksVO.setCreateTime(timeStr);
            worksVOList.add(worksVO);
        });
        return worksVOList;
    }

    @Override
    public List<WorksVO> getListWorksByID(Long uid, Long isShow) {
        List<Works> worksList = worksMapper.userSelectById(uid, isShow);
        List<WorksVO> worksVOList = new ArrayList<>();
        worksList.forEach(works -> {
            WorksVO worksVO = new WorksVO();
            BeanUtils.copyProperties(works, worksVO);
            Timestamp createTime = works.getCreateTime();
            //格式化时间：封装好的工具
            String timeStr = StrUtil.formattedTime(createTime);
            worksVO.setCreateTime(timeStr);
            worksVOList.add(worksVO);
        });
        return worksVOList;
    }

    @Override
    public List<WorksVO> getAllListWorksByID(Long uid) {
        List<Works> worksList = worksMapper.selectById(uid);
        List<WorksVO> worksVOList = new ArrayList<>();
        worksList.forEach(works -> {
            WorksVO worksVO = new WorksVO();
            BeanUtils.copyProperties(works, worksVO);
            Timestamp createTime = works.getCreateTime();
            //格式化时间：封装好的工具
            String timeStr = StrUtil.formattedTime(createTime);
            worksVO.setCreateTime(timeStr);
            worksVOList.add(worksVO);
        });
        return worksVOList;
    }

    @Override
    public WorksVO getOneWorksByID(Long wid) {
        WorksVO worksVO = new WorksVO();
        Works works = worksMapper.selectOneById(wid);
        if (works != null) {
            BeanUtils.copyProperties(works, worksVO);
            Timestamp createTime = works.getCreateTime();
            //格式化时间：封装好的工具
            String timeStr = StrUtil.formattedTime(createTime);
            worksVO.setCreateTime(timeStr);
        }
        return worksVO;
    }

    @Override
    public List<CategoryVO> getCategoryByWorksID(Long wid) {
        //先查询tb_works_category有什么类别ID
        //根据这些类别ID去tb_category查询所有的类别
        //SELECT *  FROM tb_category WHERE id in (SELECT category_id FROM tb_works_category WHERE works_id = 2)
        return worksMapper.getCategoryByWorks(wid);
    }

    @Override
    public List<WorksVO> getWorksByIsShow(Long isShow) {
        List<WorksVO> worksVOList = new ArrayList<>();
        Long currentId = BaseContext.getCurrentId();
        List<Works> worksList  = worksMapper.selectWorksIsShow(currentId,isShow);
        worksList.forEach(works -> {
            WorksVO worksVO = new WorksVO();
            BeanUtils.copyProperties(works,worksVO);
            worksVO.setCreateTime(StrUtil.formattedTime(works.getCreateTime()));
            worksVOList.add(worksVO);
        });

        return worksVOList;
    }

    @Override
    public boolean EditWorksIsShow(Long worksId, Long isShow) {
        worksMapper.updateIsShow(worksId,isShow);
        return true;
    }

    @Override
    public Long getWorksTotal(Long uid) {
        return worksMapper.totalWorksByUid(uid);
    }


    @Override
    public WorksVO getOneWorks(Long wid) {
        WorksVO worksVO = new WorksVO();
        Long currentId = BaseContext.getCurrentId();

        Works works = worksMapper.selectByIdAndWid(wid, currentId);
        if (works != null) {
            BeanUtils.copyProperties(works, worksVO);
            Timestamp createTime = works.getCreateTime();
            //格式化时间：封装好的工具
            String timeStr = StrUtil.formattedTime(createTime);
            worksVO.setCreateTime(timeStr);
        }
        return worksVO;
    }

    @Override
    public List<WorksVO> getWorksByTitle(String value) {
        List<WorksVO> worksVOList = new ArrayList<>();
        Long currentId = BaseContext.getCurrentId();
        List<Works> worksList = worksMapper.selectByIdAndName(value, currentId);

        worksList.forEach(works -> {
            WorksVO worksVO = new WorksVO();
            BeanUtils.copyProperties(works, worksVO);
            Timestamp createTime = works.getCreateTime();
            //格式化时间：封装好的工具
            String timeStr = StrUtil.formattedTime(createTime);
            worksVO.setCreateTime(timeStr);

            worksVOList.add(worksVO);
        });
        return worksVOList;
    }

    @Override
    public boolean AddWorks(WorksDTO worksDTO) {
        Long currentId = BaseContext.getCurrentId();
        Works works = new Works();
        BeanUtils.copyProperties(worksDTO, works);
        works.setUserId(currentId);
        works.setCreateTime(StrUtil.getTimestamp());
        log.info("添加作品works对象：{}", works);
        worksMapper.insert(works);
        return true;
    }

    @Override
    public boolean EditWorks(WorksDTO worksDTO) {
        Long currentId = BaseContext.getCurrentId();
        Works works = new Works();
        BeanUtils.copyProperties(worksDTO, works);
        works.setUserId(currentId);
        //不用更改创建时间
        log.info("更改作品works对象：{}", works);
        worksMapper.update(works);
        return true;
    }

    @Override
    public boolean DeleteWorks(Long id) {
        worksMapper.deleteById(id);
        return false;
    }

    @Override
    public List<WorksVO> worksByCategory(Long categoryId) {
        List<WorksVO> worksVOList = new ArrayList<>();
        Long currentId = BaseContext.getCurrentId();
        List<Works> list = worksMapper.worksByCategory(categoryId, currentId);
        list.forEach(works -> {
            WorksVO worksVO = new WorksVO();
            BeanUtils.copyProperties(works, worksVO);
            String time = StrUtil.formattedTime(works.getCreateTime());
            worksVO.setCreateTime(time);
            worksVOList.add(worksVO);
        });
        return worksVOList;
    }

    @Override
    public List<WorksVO> worksByNoCategory() {

        List<WorksVO> worksVOList = new ArrayList<>();
        Long currentId = BaseContext.getCurrentId();
        List<Works> list = worksMapper.worksByNoCategory(currentId);
        list.forEach(works -> {
            WorksVO worksVO = new WorksVO();
            BeanUtils.copyProperties(works, worksVO);
            String time = StrUtil.formattedTime(works.getCreateTime());
            worksVO.setCreateTime(time);
            worksVOList.add(worksVO);
        });
        return worksVOList;
    }

    @Override
    public List<CategoryVO> getWorksByNoCategory(Long worksId) {
        Long currentId = BaseContext.getCurrentId();
        List<CategoryVO> categoryVOList = new ArrayList<>();
        List<Category> categoryList = worksMapper.getNoCategory(worksId, currentId);
        categoryList.forEach(category -> {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(category, categoryVO);
            categoryVOList.add(categoryVO);
        });
        return categoryVOList;
    }

    @Override
    public List<CategoryVO> getWorksByCategory(Long worksId) {
        Long currentId = BaseContext.getCurrentId();
        List<CategoryVO> categoryVOList = new ArrayList<>();
        List<Category> categoryList = worksMapper.getCategory(worksId, currentId);
        categoryList.forEach(category -> {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(category, categoryVO);
            categoryVOList.add(categoryVO);
        });
        return categoryVOList;
    }

    @Override
    public boolean AddCategoryByWorks(AddCategoryByWorksDTO addCategoryByWorksDTO) {

        //先去 tb_works_category表 删除这个作品的所有类别
        worksCategoryMapper.deleteByWorksId(addCategoryByWorksDTO.getCurrentWorksId());
        //再去添加这个作品要添加的所有类别
        worksCategoryMapper.insertBatch(addCategoryByWorksDTO.getCategoryIds(), addCategoryByWorksDTO.getCurrentWorksId());
        return true;
    }


}
